<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-form ref="form" :model="user" label-width="80px">
        <el-form-item label="用户名">
            <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="user.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
    </el-form>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="./js/utils.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                user: {
                    username: null,
                    password: null
                }
            }
        },
        methods: {
            onSubmit() {
                console.log('submit!');
                // 保存或者更新书本信息
                this.saveOrUpdate();
            },
            saveOrUpdate() {
                let url = '/user/login';
                // 特别注意这个
                var self = this;
                console.log(self.user);
                axios.post(url, self.user)
                    .then(function (res) {
                        console.log(res.data);
                        if(res.data.ok){
                            window.location.href = "/index.html";
                        }else {
                            self.$message.error(res.data.msg);
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                        self.$message.error('请求失败');
                    });
            }
        }
    })
</script>
</html>